package com.fit.exercise.msgp2p;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public abstract class FriendsList_DlgAdd extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 953242178187393807L;
	private List<PersonInfo> friendList;

	public FriendsList_DlgAdd() {
		// Get friends list
		friendList = T2Messenger.friendList;
		initGUI();
	}

	private void initGUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		Container contentpane = getContentPane();
		contentpane.setLayout(new FlowLayout());

		TableModel dataModel = new AbstractTableModel() {
			public int getColumnCount() {
				return 3;
			}

			public int getRowCount() {
				return friendList.size();
			}

			public String getValueAt(int row, int col) {
				switch (col) {
				case 0:
					return friendList.get(row).getName();
				case 1:
					return friendList.get(row).getIpAddress();
				case 2:
					return friendList.get(row).getPort().toString();
				}

				return "";
			}
		};
		final JTable table = new JTable(dataModel);
		JScrollPane scroll = new JScrollPane(table);

		JButton btnIntroduce = new JButton("Send request");
		btnIntroduce.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				sendData(table);
				close();
			}

		});

		contentpane.add(scroll);
		contentpane.add(btnIntroduce);
		setContentPane(contentpane);
		setVisible(true);
		setBounds(0, 0, 500, 500);
	}

	public void close() {
		this.dispose();
	}

	protected abstract void sendData(final JTable table);

}
